//
//  UIView+Help.m
//  Carpooling
//
//  Created by haidong on 2016/10/26.
//  Copyright © 2016年 MagicBeans. All rights reserved.
//

#import "UIView+Help.h"

@implementation UIView (Help)

//重写系统的方法时使用
//- (instancetype)init {
//    if (self = [super init]) {
//
//    }
//    return self;
//}

//orgin
- (void)setOrigin:(CGPoint)orgin
{
    self.frame = CGRectMake(orgin.x, orgin.y, self.frame.size.width, self.frame.size.height);
}
- (CGPoint)origin
{
    return self.frame.origin;
}

- (void)setOriginX:(CGFloat)originX
{
    self.frame = CGRectMake(originX, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
}
- (CGFloat)originX
{
    return self.frame.origin.x;
}

- (void)setOriginY:(CGFloat)originY
{
    self.frame = CGRectMake(self.frame.origin.x, originY, self.frame.size.width, self.frame.size.height);
}
- (CGFloat)originY
{
    return self.frame.origin.y;
}

//size
- (void)setSizeHeight:(CGFloat)sizeHeight
{
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, sizeHeight);
}
- (CGFloat)sizeHeight
{
    return self.frame.size.height;
}

- (void)setSizeWidth:(CGFloat)sizeWidth
{
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, sizeWidth, self.frame.size.height);
}
- (CGFloat)sizeWidth
{
    return self.frame.size.width;
}

//center
- (void)setFloatx:(CGFloat)floatx
{
    self.center = CGPointMake(floatx, self.center.y);
}
- (CGFloat)floatx
{
    return self.center.x;
}

- (void)setFloaty:(CGFloat)floaty
{
    self.center = CGPointMake(self.center.x, floaty);
}
- (CGFloat)floaty
{
    return self.center.y;
}

- (void)setBottom:(CGFloat)bottom
{
    self.originY = bottom - self.sizeHeight;
}
- (CGFloat)bottom
{
    return self.originY+self.sizeHeight;
}

- (void)setRight:(CGFloat)right
{
    self.originX = right - self.sizeWidth;
}
- (CGFloat)right
{
    return self.originX+self.sizeWidth;
}

- (CGFloat)midX {
    return CGRectGetMidX(self.frame);
}
- (void)setMidX:(CGFloat)midX {
    self.frame = CGRectMake(midX - self.sizeWidth / 2.0, self.originY, self.sizeWidth, self.sizeHeight);
}

- (CGFloat)midY {
    return CGRectGetMidY(self.bounds);
}
- (void)setMidY:(CGFloat)midY {
    self.frame = CGRectMake(self.originX, midY - self.sizeHeight / 2.0, self.sizeWidth, self.sizeHeight);
}


- (void)setCorner:(CGFloat)aCorner {
    self.layer.cornerRadius = aCorner;
    self.layer.masksToBounds = YES;
}

- (void)setBorderWidth:(CGFloat)aBorderWidth borderColor:(UIColor *)aBorderColor {
    self.layer.borderWidth = aBorderWidth;
    self.layer.borderColor = aBorderColor.CGColor;
}

- (id)getViewController {
    for (UIView *next = [self superview]; next; next = [next superview]) {
        UIResponder *responder = [next nextResponder];
        if ([responder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)responder;
        }
    }
    return nil;
}

+ (UIView *)loadFromNib {
    char *cName = (char *)object_getClassName(self);
    NSString *nibName = [[NSString alloc] initWithUTF8String:cName];
    UIView *view = [[[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil] lastObject];
    return view;
}

+ (UIView *)loadNibWithNibName:(NSString *)nibName {
    if (nibName == nil) {
        char *cName = (char *)object_getClassName(self);
        nibName = [[NSString alloc] initWithUTF8String:cName];
    }
    UIView *view = [[[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil] lastObject];
    return view;
}

@end
